<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 全排列（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <p id="main-toc"><strong>目录</strong></p> 
<p id="%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0-toc" style="margin-left:80px;"><a href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0" rel="nofollow">题目描述</a></p> 
<p id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0-toc" style="margin-left:80px;"><a href="#%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0" rel="nofollow">输入描述</a></p> 
<p id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0-toc" style="margin-left:80px;"><a href="#%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0" rel="nofollow">输出描述</a></p> 
<p id="%E7%94%A8%E4%BE%8B-toc" style="margin-left:80px;"><a href="#%E7%94%A8%E4%BE%8B" rel="nofollow">用例</a></p> 
<p id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90-toc" style="margin-left:80px;"><a href="#%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90" rel="nofollow">题目解析</a></p> 
<p id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81-toc" style="margin-left:80px;"><a href="#%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" rel="nofollow">算法源码</a></p> 
<hr id="hr-toc" /> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0">题目描述</h4> 
<p>给定一个只包含大写英文字母的字符串S&#xff0c;要求你给出对S重新排列的所有不相同的排列数。</p> 
<p>如&#xff1a;S为ABA&#xff0c;则不同的排列有ABA、AAB、BAA三种。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入一个长度不超过10的字符串S&#xff0c;我们确保都是大写的。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出S重新排列的<strong>所有不相同的排列数</strong>&#xff08;包含自己本身&#xff09;。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">ABA</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">ABCDEFGHHA</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">907200</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题是一个数学问题。即求解无重复的全排列个数。</p> 
<p>首先我们可以求解出有重复的全排列个数&#xff0c;假设共有n个元素&#xff0c;则有重复的全排列个数为n!个。比如ABA有三个元素&#xff0c;则有重复的全排列个数为3! &#61; 3x2x1 &#61; 6。</p> 
<p>然后&#xff0c;我们需要找出重复的元素的个数&#xff0c;比如ABA中的A重复了2次&#xff0c;因此会生成2&#xff01;组相同排列。</p> 
<p>因此不重复的全排列个数有 3&#xff01;/ 2! &#61; 3个。</p> 
<p></p> 
<p>假设一共n个元素&#xff0c;其中某元素α重复x次&#xff0c;某元素β重复了y次&#xff0c;则最终不重复全排列个数有&#xff1a;</p> 
<p>n! / x! / y! 个</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  let total &#61; getFact(line.length);

  const obj &#61; {};
  for (let c of line) {
    obj[c] ? obj[c]&#43;&#43; : (obj[c] &#61; 1);
  }

  for (let k in obj) {
    if (obj[k] &gt; 1) {
      total /&#61; getFact(obj[k]);
    }
  }

  console.log(total);
});

function getFact(n) {
  let fact &#61; 1;
  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    fact *&#61; i;
  }
  return fact;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashMap;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String s &#61; sc.next();
    System.out.println(getResult(s));
  }

  public static int getResult(String s) {
    int total &#61; getFact(s.length());

    HashMap&lt;Character, Integer&gt; count &#61; new HashMap&lt;&gt;();
    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      char k &#61; s.charAt(i);
      count.put(k, count.getOrDefault(k, 0) &#43; 1);
    }

    for (Character k : count.keySet()) {
      int n &#61; count.get(k);
      if (n &gt; 1) total /&#61; getFact(n);
    }

    return total;
  }

  public static int getFact(int n) {
    int fact &#61; 1;
    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) fact *&#61; i;
    return fact;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()


def getFact(n):
    fact &#61; 1
    for i in range(1, n &#43; 1):
        fact *&#61; i
    return fact


# 算法入口
def getResult(s):
    total &#61; getFact(len(s))

    count &#61; {}
    for c in s:
        if count.get(c) is None:
            count[c] &#61; 1
        else:
            count[c] &#43;&#61; 1

    for c in count.keys():
        n &#61; count[c]
        if n &gt; 1:
            total /&#61; getFact(n)

    return int(total)


# 算法调用
print(getResult(s))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>